﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccessLayer;
using DataAccessLayer.DAL;
using Common;

namespace BestBuy.ManagerViews
{
    public partial class Category : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null || user.Status != (int)UserStatus.Active || user.RoleID != (int)RoleType.Manager)
            {
                Response.Redirect("~/Account/Login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                LoadPage();
            }
        }

        private void LoadPage()
        {
            LoadTreeView();
            LoadCombobox();
        }

        private void LoadInfoProduct()
        {
            int id = int.Parse(tvCategories.SelectedNode.Value);
            ProductCategory pro = ManagerDAL.GetByID(id);
            if (pro != null)
            {
                txtName.Text = pro.Name;
                txtDescription.Text = pro.Description;
                if (pro.ParentID != null)
                    cbParent.SelectedValue = pro.ParentID.ToString();
                else
                    cbParent.SelectedIndex = 0;
            }
        }

        private void LoadTreeView()
        {
            tvCategories.Nodes.Clear();
            List<ProductCategory> list = ManagerDAL.GetAllCategories();
            TreeNode root = new TreeNode();
            root.Value = "0";
            root.Text = "Category";
            CreateCategory(list, root, null);
            tvCategories.Nodes.Add(root);
        }
      
        private void CreateCategory(List<ProductCategory> list, TreeNode parent, int? parentID)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ParentID == parentID)
                {
                    TreeNode treenode = new TreeNode();
                    treenode.Value = list[i].ID.ToString();
                    treenode.Text = list[i].Name;
                    CreateCategory(list, treenode, list[i].ID);
                    parent.ChildNodes.Add(treenode);
                }
            }

            //return treenode;
        }

        protected void tvCategories_SelectedNodeChanged(object sender, EventArgs e)
        {
            if(tvCategories.SelectedNode != null)
                LoadInfoProduct();
        }

        private void LoadCombobox()
        {
            ClearCombobox();
            List<ProductCategory> list = ManagerDAL.GetAllCategories();
            cbParent.DataSource = list;
            cbParent.DataTextField = "Name";
            cbParent.DataValueField = "ID";
            cbParent.DataBind();

        }
        
        private void ClearCombobox()
        {
            for (int i = cbParent.Items.Count - 1; i > 0; i--)
            {
                cbParent.Items.RemoveAt(i);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ProductCategory item = new ProductCategory();
            item.ID = int.Parse(tvCategories.SelectedNode.Value);
            item.Name = txtName.Text.Trim();
            item.Description = txtDescription.Text.Trim();
            if(cbParent.SelectedIndex > 0)
                item.ParentID = int.Parse(cbParent.SelectedValue);

            if (ManagerDAL.UpdateProductCategory(item))
            {
                ManagerInfoMessage.Show("Update Product Category success.", UserControls.MessagePanel.MessageType.Success);
            }
            else
            {
                ManagerInfoMessage.Show("Update Product Category error.", UserControls.MessagePanel.MessageType.Error);
            }
            LoadPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
                int id = int.Parse(tvCategories.SelectedValue);
                if (ManagerDAL.DeleteProductCategory(id))
                {
                    ManagerInfoMessage.Show("Delete Product Category success.", UserControls.MessagePanel.MessageType.Success);
                }
                else
                {
                    ManagerInfoMessage.Show("Delete Product Category error.", UserControls.MessagePanel.MessageType.Error);
                }
                LoadPage();
        }

        
    }
}